$(function () {
    var thisPage = null,
        html = [],
        user = null,
        myScroll = null,
        pullUp = null,
        pullDown = null,
        query = {
            index: 1,
            size: 10,
            query: "",
            search: {},
            sorting: {}
        },
        thispage = null;

    var invitelistPage = function () {
        this.init();
    };

    invitelistPage.prototype = {
        init: function () {
            this.addScroll();
            this.initData();
            this.bindEvent();
        },
        initData: function () {
            user = getLocalStorage(STORAGE_MANAGER.user);
            if (!user || !user.id) {
                redirect("../account/login.html");
                return false;
            }
            this.loadInviteList();
        },
        addScroll: function () {
            //添加滚动条
            pullUp = $("#pullUp");
            pullDown = $("#pullDown");
            pullUp.hide();
            pullDown.hide();
            myScroll = new IScroll('#wrapper', {
                probeType: 2,
                scrollbars: true,
                mouseWheel: false,
                fadeScrollbars: true,
                bounce: true,
                interactiveScrollbars: false,
                shrinkScrollbars: 'scale',
                click: true,
                keyBindings: true,
                momentum: true
            });
            myScroll.on("scrollEnd", function () {
                if ((this.y < 0) && (this.y == this.maxScrollY)) {
                    console.log("滚动到底部");
                    pullUp.show();
                    setTimeout(function () {
                        query.index++;
                        thispage.loadInviteList();
                    }, 500);
                } else if ((this.y == 0) && (this.y != this.maxScrollY)) {
                    console.log("滚动到顶部");
                    pullDown.show();
                    setTimeout(function () {
                        query.index--;
                        if (query.index < 0) {
                            query.index = 0;
                        }
                        thispage.loadInviteList();
                    }, 500);
                }
            });
        },
        loadInviteList: function () {
            $.ajax({
                type: "POST",
                url: CONFIG_MANAGER.RequstUrl + "v1/member/invite/" + user.id,
                async: true,
                data: JSON.stringify(query),
                headers: {"token": user.token},
            }).done(function (data) {
                var data = JSON.parse(data), invist = data.data, i = 0, html = [];
                while (invist[i]) {
                    html.push('<div class="yqjlk mbbottom">');
                    html.push('<div class="ygjlbg">');
                    html.push('<p class="yqzccg">注册成功</p>');
                    html.push('<p class="yqhyny"><span>￥' + invist[i].money + '</span>好友：' + invist[i].nickname + '</p>');
                    html.push('<p class="yqhyny yqhyny1"><span>奖励至钱包余额</span>' + invist[i].createtime + '</p>');
                    html.push('</div>');
                    html.push('</div>');
                    i++;
                }
                $("#inviteList").html(html.join(''));
                //加载完成后隐藏上拉下拉提示
                setTimeout(function () {
                    myScroll.refresh();
                    pullUp.hide();
                    pullDown.hide();
                }, 100);
            }).fail(function (data) {
                var infor = JSON.parse(data);
                $.alert(infor.message);
            });
        },
        bindEvent: function () {
            //回退
            $("#back").on("tap", function () {
                back();
            });

        }
    };

    thispage = new invitelistPage();

});
